/*
 * Copyright (c) 2015 MONKEYK Information Technology Co. Ltd
 * www.monkeyk.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * MONKEYK Information Technology Co. Ltd ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement you
 * entered into with MONKEYK Information Technology Co. Ltd.
 */
package com.idsmanager.oauth.service.oauth.validate;

import com.idsmanager.commons.utils.MatchUtils;
import com.idsmanager.zdh.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * 2016/3/5
 *
 * @author Shengzhao Li
 */
public abstract class AbstractUserValidator implements Validator {


    protected static final int MIN_PASSWORD_LENGTH = 8;

    @Autowired
    protected UserService userService;


    protected void validatePassword(final String password, Errors errors) {

        if (StringUtils.isEmpty(password)) {
            errors.rejectValue(passwordField(), "abstract.user.validate.password", "Password is required");
            return;
        }

        if (password.length() < MIN_PASSWORD_LENGTH) {
            errors.rejectValue(passwordField(), "abstract.user.validate.password.length", "Password length must be >= " + MIN_PASSWORD_LENGTH);
        }

    }


    protected void validatePassword(final String password, final String rePassword, Errors errors) {

        if (StringUtils.isEmpty(password)) {
            errors.rejectValue("password", "abstract.user.validate.password", "Password is required");
            return;
        }

        if (password.length() < MIN_PASSWORD_LENGTH) {
            errors.rejectValue("password", "abstract.user.validate.password.length", "Password length must be >= " + MIN_PASSWORD_LENGTH);
            return;
        }

        if (!password.equals(rePassword)) {
            errors.rejectValue("rePassword", "abstract.user.validate.repassword", "Re-Password must be the same");
        }

    }

    protected String passwordField() {
        return "password";
    }


    protected void validateEmail(String email, Errors errors) {

        if (StringUtils.isEmpty(email)) {
            errors.rejectValue(emailField(), "abstract.user.validate.email.empty", "Email is required");
            return;
        }

        if (!MatchUtils.isEmail(email)) {
            errors.rejectValue(emailField(), "abstract.user.validate.email.invalid", "Invalid email format");
            return;
        }


        boolean existed = userService.isExistedUsername(email);
        if (existed) {
            errors.rejectValue(emailField(), "abstract.user.validate.email.existed", "Email already existed");
        }
    }

    protected void validateEmail(String email, String existedEmail, Errors errors) {

        if (StringUtils.isEmpty(email)) {
            errors.rejectValue(emailField(), "abstract.user.validate.email.empty", "Email is required");
            return;
        }

        if (!MatchUtils.isEmail(email)) {
            errors.rejectValue(emailField(), "abstract.user.validate.email.invalid", "Invalid email format");
            return;
        }

        if (!email.equals(existedEmail)) {
            boolean existed = userService.isExistedUsername(email);
            if (existed) {
                errors.rejectValue(emailField(), "abstract.user.validate.email.existed", "Email already existed");
            }
        }
    }

    protected String emailField() {
        return "email";
    }


}
